package UseStream;

import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class StreamFinalMethod {
    public static void main(String[] args) {
        // 终结方法的操作
        // threeMethod();
        collectMethod();

    }
    public static void threeMethod(){
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"奥利给","给力奥","退休生活","美滋滋","什么时候也可以");
        // 方法一: forEach 遍历流
        list.stream().forEach(s -> System.out.println(s));
        // 方法二: count获取
        long count = list.stream().count();
        System.out.println(count);
        // 方法三: toArray收集到数组中去
        Object[] array = list.stream().toArray(); // 无参构造返回object
        String[] array1 = list.stream().toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        }); // 有参构造就是，有参创建一个数据转stream流中的数据
        // lambada书写方式
        String[] array2 = list.stream().toArray(value -> new String[value]);
        System.out.println(Arrays.toString(array2));

    };

    public static void collectMethod(){
        // 存到list set map
        // 构建数据
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"小平-女-18","小同-女-19","小肯-男-20","小结-男-21");
        // 存list
        List<String> arr01 = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toList());
        //System.out.println(arr01);
        // 存set
        Set<String> arr02 = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toSet());
        //System.out.println(arr02);
        Map<String, Integer> arr03 = list.stream().collect(Collectors.toMap(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s.split("-")[0];
            }
        }, new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                String age = s.split("-")[2];
                return Integer.parseInt(age);
            }
        }));
        // System.out.println(arr03);
        // lambada方式
        Map<String, Integer> arr04 = list.stream().collect(Collectors.toMap(s -> {
            return s.split("-")[0];
        }, s -> {
            String age = s.split("-")[2];
            return Integer.parseInt(age);
        }));
        System.out.println(arr04);

    }
}
